//欢迎页，将用户最关注的一些数据聚合在此
"use strict"
exports.index = async function(){
    //console.log(this.req);
    this.view = "branch/welcome";
    let branch=this.branch;
    //拉出服务器
    let server = this.model.branch("server",branch);
    let keys = ["name","status"]
    let ret= await server.mget(keys);
    let data={server:[]};
    let query_list=[];
    for(let k in ret){
        let v = ret[k];
        let d={};
        if(v._id>=900){
            continue;
        }
        if(v.status==9){
            continue;
        }
        d.sid=v._id;
        d.sname=v.name;
        data.server.push(d);
        try{
            query_list.push(this.library("game/request",branch,d.sid,'/queryWelcome/total',{}));
        }catch(err){
            console.log(err);
        }
    }

    let rets=await Promise.all(query_list);

    for(let i=0;i<rets.length;++i){
        if(rets[i]){
            data.server[i]=Object.assign(data.server[i],rets[i]);
        }
        else{
            data.server[i]=Object.assign(data.server[i],{num:0,pnum:0,anum:0,income:0});
        }
    }
    data['bpath'] = ['', 'b', branch].join('/');
   // console.log(data);
    //总收入

    //总用户数-付费用户-活跃用户
//    console.log(data);

    data['setting']={};
    let setting = this.model.admin('setting');
    let setting_ret=await setting.find({});
    if(setting_ret){
        data.setting=Object.assign(data.setting,setting_ret);        
    }

    data['auth']={};
    let auth = this.model.admin('authorize');
    let auth_ret=await auth.find({"uid":this.session.uid});
    if(auth_ret){
        data.auth=Object.assign(data.auth,auth_ret);        
    }
    return data;
}

exports.query = async function(){
	this.output='json';
    return 'ok'
}